package com.runewbie.pafcmall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.runewbie.pafcmall.product.entity.ProductAttrValueEntity;
import com.runewbie.pafcmall.product.service.ProductAttrValueService;
import com.runewbie.pafcmall.product.vo.AttrRespVo;
import com.runewbie.pafcmall.product.vo.AttrVo;
import com.runewbie.pafcmall.product.vo.ProductAttrValueVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.runewbie.pafcmall.product.service.AttrService;
import com.runewbie.common.utils.PageUtils;
import com.runewbie.common.utils.R;


/**
 * 商品属性
 *
 * @author runewbie
 * @email 0102lijian@gmail.com
 * @date 2020-04-12 00:54:30
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    ProductAttrValueService productAttrValueService;

    /**
     * 获取spu规格
     *
     * @param spuId
     * @return
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrListForSpu(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrListForSpu(spuId);

        List<ProductAttrValueVo> vos = entities.stream().map(productAttrValue -> {
            ProductAttrValueVo vo = new ProductAttrValueVo();
            BeanUtils.copyProperties(productAttrValue, vo);
            return vo;
        }).collect(Collectors.toList());

        return R.ok().put("data", vos);
    }

    /**
     * 获取分类规格参数 & 获取分类销售属性
     * <p>
     * 获取分类规格参数: /product/attr/base/list/{catelogId}
     * 获取分类销售属性: /product/attr/sale/list/{catelogId}
     *
     * @param params
     * @param catelogId 分类id
     * @return
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType") String type) {
        PageUtils page = attrService.queryBaseAttrPage(params, catelogId, type);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息：查询属性详情
     */
    @GetMapping("/info/{attrId}")
//    @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {
//		AttrEntity attr = attrService.getById(attrId);
        AttrRespVo respVo = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", respVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr) {
        attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr) {
        attrService.updateAttrVo(attr);

        return R.ok();
    }

    /**
     * 修改商品规格
     *
     * @param spuId
     * @param vos
     * @return
     */
    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueVo> vos) {

        productAttrValueService.updateSpuAttr(spuId, vos);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
